﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FileTranslator
{
    public class WholeTableViewWrapper : DataTableWrapper
    {
        #region field
        public List<DataTableWrapper> ChildDataTableWrapper { get; set; }
        #endregion
        
        #region Constructor
        public WholeTableViewWrapper(ConstraintHelper ch, List<DataTableWrapper> dtws)
            :base(ch)
        {
            ChildDataTableWrapper = dtws;
        }
        #endregion

        public override bool VerifyConstraint()
        {
            // 依次做以此表作为DataView依据的表的VerifyConstraint
            bool result = true;

            foreach (DataTableWrapper dtw in ChildDataTableWrapper)
            {
                result &= dtw.VerifyConstraint();
                if (!result)
                {
                    return false;
                }
            }

            return result;
        }

        public override bool Change(int position, DataRow newDataRow)
        {
            // 首先检查position在哪个子DataView中
            int index;
            DataTableWrapper dtwOfPosition;
            FindChildDataTableWrapper(position, out index, out dtwOfPosition);
            
            // 执行子DataTableWrapper的Change
            if (dtwOfPosition != null)
            {
                return dtwOfPosition.Change(index, newDataRow);
            }

            return false;
        }

        private void FindChildDataTableWrapper(int position, out int index, out DataTableWrapper dtwOfPosition)
        {
            index = 0;
            int beginIndex = 0;
            dtwOfPosition = null;
            foreach (DataTableWrapper dtw in ChildDataTableWrapper)
            {
                beginIndex = index;
                index += dtw.InnerTable.Count;
                if (index > position)
                {
                    // position所指的行在此DataTableWrapper中
                    dtwOfPosition = dtw;

                    // 现在index表示position所指代的行在子DataTableWrapper中的位置
                    index = position - beginIndex;
                    break;
                }
            }
        }

        public override bool Delete(int position)
        {
            // 首先检查position在哪个子DataView中
            int index;
            DataTableWrapper dtwOfPosition;
            FindChildDataTableWrapper(position, out index, out dtwOfPosition);

            // 执行子DataTableWrapper的Delete
            if (dtwOfPosition != null)
            {
                return dtwOfPosition.Delete(index);
            }

            return false;
        }

        public override bool InsertAt(int position)
        {
            // 首先检查position在哪个子DataView中
            int index;
            DataTableWrapper dtwOfPosition;
            FindChildDataTableWrapper(position, out index, out dtwOfPosition);

            // 执行子DataTableWrapper的InsertAt
            if (dtwOfPosition == null)
            {
                // 说明想要插入的地方是在整张表的最后,即，最后一个ChildDataWraper
                dtwOfPosition = ChildDataTableWrapper[ChildDataTableWrapper.Count - 1];
            }

            return dtwOfPosition.InsertAt(index);
        }

        public override bool Move(int position, int offset)
        {
            // 首先检查position在哪个子DataView中
            int index;
            DataTableWrapper dtwOfPosition;
            FindChildDataTableWrapper(position, out index, out dtwOfPosition);

            // 执行子DataTableWrapper的Move
            if (dtwOfPosition != null)
            {
                return dtwOfPosition.Move(index, offset);
            }

            return false;
        }
    }
}
